python sort list by rule

39

# adapted from answer given by Stack Overflow user in the source link

my_list = list(...) # this is your list

# my_rule() is a custom function applied on elements of list that must return a number
# in this way, the "real" sorting will be performed by considering the outcome of my_rule()
# reverse: False to sort element in ascending order
rule_sorted_list = sorted(my_list, lambda x: my_rule(x), reverse = False)

# Note: if your list contains number only, then just write sorted(my_list) to sort it

Comments

Submit
0 Comments